﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
 
namespace Outlook.WorkItems.Extension.Controls
{
    public partial class DownloadAttachmentsForm : Form
    {
        private List<KeyValuePair<string, string>> _wssDocumentsToDownload;

        public List<KeyValuePair<string, string>> WssDocumentsToDownload
        {
            get { return _wssDocumentsToDownload; }
        }

        public bool InsertAsHtmlLink
        {
            get { return radioButton2.Checked; }
        }

        public DownloadAttachmentsForm()
        {
            InitializeComponent();
            teamTreeViewUC1.ShowFile = true;
            teamTreeViewUC1.InitTreeView();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                _wssDocumentsToDownload = new List<KeyValuePair<string, string>>();

                Cursor = Cursors.WaitCursor;

                var checkedFilesNode = teamTreeViewUC1.treeView1.Nodes.FindNodes(n => n is FileNode && n.Checked);

                foreach (var item in checkedFilesNode.OfType<FileNode>())
                {
                    string filePath = item.FilePath;

                    _wssDocumentsToDownload.Add(new KeyValuePair<string,string>(filePath, item.TeamServerNode.Tag.ToString()));
                }
                Close();
            }
            catch (Exception ex)
            {
                using (ErrorForm frm = new ErrorForm(ex))
                {
                    frm.ShowDialog();
                }
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == radioButton1.Checked)
            {
                if (sender == radioButton1)
                    radioButton2.Checked = !radioButton1.Checked;
                else
                    radioButton1.Checked = !radioButton2.Checked;
            }
        }
    }
}
